package com.trkj.exam;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.trkj.base.BaseDataRefreshFragment;
import com.trkj.base.Constants;
import com.trkj.base.User;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;

import java.text.MessageFormat;

/**
 * 过期练习
 * Created by LiZanhong on 2015/06/16.
 */
public class ExpiredExerciseFragment extends BaseDataRefreshFragment {
    private int userId;
    private int deptId;

    public int getUserId() {
        User user= UserService.getInstance(this.getActivity()).parseUser();
        return user == null ? 0 : user.userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public int getDeptId() {
        User user= UserService.getInstance(this.getActivity()).parseUser();
        return user == null ? 0 : user.deptId;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //先定义Adapter
        if(getAdapter() == null) {
            setAdapter(new ExpiredExerciseAdapter());
        }
        View root = super.onCreateView(inflater, container, savedInstanceState);
        return root;
    }


    @Override
    public String getUrl() {
        String url = MessageFormat.format(
                Constants.ApiUrl.EXPIRE_PRACTICE,
                getDeptId(),
                getUserId()
        );
        Log.i("URL", url);
        return url;
    }


    class ExpiredExerciseAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return getData().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return getData().getJSONObject(position).getIntValue("exam_id");
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity())
                        .inflate(R.layout.fragment_expired_exericse_item, null);
                viewHolder = new ViewHolder();
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.outDate = (TextView) convertView.findViewById(R.id.out_date);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            JSONArray data = getData();

            viewHolder.title.setText(data.getJSONObject(position).getString("exam_name"));
                viewHolder.outDate.setText("过期：" +
                        data.getJSONObject(position).getString("date_to").substring(0, 10));
            return convertView;
        }

        class ViewHolder {
            TextView title;
            TextView outDate;
        }
    }

}
